/**
 * 
 */
package leetCode;

import java.util.ArrayList;

/**
 * @author zhongfang
 *
 */
public class baseballgame {
	public int calPoints(String[] ops) {
		ArrayList<Integer> lasts = new ArrayList<>();
		int sum = 0;
		for (String op : ops) {
			int newVal = 0;
			if (op.equals("C")) {
				// 移除last
				sum -= lasts.remove(lasts.size() - 1);
			} else {
				if (op.equals("D")) {

					newVal = 2 * lasts.get(lasts.size() - 1);
				} else if (op.equals("+")) {
					newVal = lasts.get(lasts.size() - 1) + lasts.get(lasts.size() - 2);
				} else {
					newVal = Integer.parseInt(op);
				}
				sum += newVal;
				lasts.add(newVal);
			}
		}
		return sum;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] ops = { "5", "-2", "4", "C", "D", "9", "+", "+" };
		System.out.println(new baseballgame().calPoints(ops));
	}

}
